$(
    function () {


        findById();
    }

)

layui.use('form',function () {
    let form = layui.form
    form.on('submit(edit)',function (data) {
        data = data.field;
        data.id=sessionStorage.getItem("newsId");
        let result =myAjax('http://localhost/ssm/back/lawyer/edit',data,'post')
        if(result.code=='200'){
            layer.alert("增加成功", {
                    icon: 6
                },
                function () {
                    //关闭当前frame
                    xadmin.close();

                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                });
        }
        else{
            layer.alert("增加失败", {
                icon: 1
            });
        }

        return false;

    })
})

function findById() {
    let id = sessionStorage.getItem("lawyerId");
    console.log(id)
    let result =myAjax('http://localhost/ssm/back/lawyer/findInfo',{id:id},'get')
    let data = result.data
    $("#head_pic").attr("src",data.head)
    $("#lawyername").val(data.lawyerName)
    createSelect(data.role )
    //$("#role option[value='" + data.dependenceObj + "']").attr("selected", true);
    $("#intro").val(data.lawyerIntro)

}

function createSelect(getRole) {
    let select = $("#role")
    layui.use('form',function () {
        let result =myAjax('http://localhost/ssm/back/lawyer/role',{},'get')
        let data = result.data
        $.each(data,function (index, item) {

            select.append(new Option(item.role,item.role))

            console.log(select)
        })
        select.val(getRole)
        layui.form.render("select");
    })
}


layui.use(['upload','form','layer'], function() {

    var $ = layui.jquery
        , upload = layui.upload;
    let form = layui.form;
    let layer = layui.layer
    let pic = $('#head_pic')



    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/upload' //改成您自己的上传接口
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {


            });
        }
        , done: function (res) {
            let result = res.data
            $('#head_pic').attr('src', result); //图片链接（base64）
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
})